package com.qipa.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * Created by kaiji on 2016/6/24.
 */
public class ResultModel {
    public final static int CODE_SUCCESS = 200;//成功
    public final static int CODE_EXCEPTION = 500;//异常
    public final static int CODE_FAILED = 1;//失败
    public final static int CODE_DATA_MISSED = 2;//数据丢失
    public final static int CODE_LOGOUT = 600;//用户未登录或者token失效
    public final static int MISSING_SERVLET_REQUEST_PARAMETER_CODE = 400;//请求参数不足
    public final static int ERROR_SERVLET_REQUEST_PARAMETER_CODE = 401;//请求参数错误
    public final static int TARGET_NOT_EXIST_CODE = 402;//目标不存在
    public final static int ILLEGAL_OPERATION = 403;//非法操作
    public final static int NO_AUTHORITY = 404;
    public final static int NO_BUILD = 1000;


    private int code;
    private String msg;
    private Object data;

    public ResultModel() {
        code = CODE_SUCCESS;
        msg = "处理成功";
    }
    public ResultModel(int code) {

        this.code = code;
    }

    public ResultModel(int code, String message) {
        this.code = code;
        this.msg = message != null ? message : "处理成功";
    }

    public ResultModel(int code, String message, Object data) {
        this(code, message);
        this.data = data;
    }


    /**
     * 把常用的返回结果封装成方法
     * 处理成功
     *
     * @return
     */
    public static ResultModel ok() {
        return new ResultModel(ResultModel.CODE_SUCCESS, "OK");
    }


    public static ResultModel ok(String message) {
        return new ResultModel(ResultModel.CODE_SUCCESS, message);
    }

    public static ResultModel ok(String message, Object data) {
        return new ResultModel(ResultModel.CODE_SUCCESS, message, data);
    }
    public static ResultModel ok(Object data) {
        return new ResultModel(ResultModel.CODE_SUCCESS, "OK", data);
    }

    /**
     * 数据丢失
     * @return
     */
    public static ResultModel dataMiss() {
        return new ResultModel(ResultModel.CODE_DATA_MISSED, "数据丢失");
    }

    /**
     * 处理失败
     *
     * @return
     */
    public static ResultModel fail() {
        return new ResultModel(CODE_FAILED, "处理失败,请稍候重试");
    }
    public static ResultModel failed() {
        return new ResultModel(CODE_FAILED, "处理失败,请稍候重试");
    }
    public static ResultModel failed(String message) {
        return new ResultModel(CODE_FAILED, message);
    }

    public static ResultModel fail(String message) {
        return new ResultModel(CODE_FAILED, message);
    }

    /**
     * 发生异常
     */
    public static ResultModel exception() {
        return new ResultModel(CODE_EXCEPTION, "系统异常");
    }
    /**
     * 发生异常
     */
    public static ResultModel exception(String message) {
        return new ResultModel(CODE_EXCEPTION, message);
    }

    /**
     * 非法操作
     */
    public static ResultModel illegalOperation() {
        return new ResultModel(ILLEGAL_OPERATION, "非法操作");
    }

    /**
     * 未登录或token失效
     */
    public static ResultModel unLogin() {
        return new ResultModel(CODE_LOGOUT, "未登录或token失效");
    }


    /**
     * 请求参数不足
     */
    public static ResultModel missingParam() {
        return new ResultModel(MISSING_SERVLET_REQUEST_PARAMETER_CODE, "请求参数不足");
    }

    public static ResultModel missingParam(String message) {
        return new ResultModel(MISSING_SERVLET_REQUEST_PARAMETER_CODE, message);
    }

    /**
     * 请求参数错误
     */
    public static ResultModel incorrectParam() {
        return new ResultModel(ERROR_SERVLET_REQUEST_PARAMETER_CODE, "请求参数错误");
    }

    public static ResultModel incorrectParam(String message) {
        return new ResultModel(ERROR_SERVLET_REQUEST_PARAMETER_CODE, message);
    }

    /**
     * 目标不存在
     */
    public static ResultModel noExist() {
        return new ResultModel(TARGET_NOT_EXIST_CODE, "目标不存在");
    }

    public static ResultModel noExist(String message) {
        return new ResultModel(TARGET_NOT_EXIST_CODE, message);
    }


    /**
     *
     */
    /**
     * 将json字符串转化为ResultModel对象
     * Object是List集合
     *
     * @param jsonData json数据
     * @param clazz    集合中的类型
     * @return ResultModel
     */

    public static ResultModel formatToList(String jsonData, Class<?> clazz) {
        try {
            if (null == clazz) {
                return format(jsonData);
            }
            JSONObject object = JSON.parseObject(jsonData);
            Object jsonArray = object.get("data");
            List<?> data = JSON.parseArray(jsonArray + "", clazz);
            return new ResultModel((Integer) object.get("code"), object.get("message") + "", data);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将json字符串转化为ResultModel对象
     * Object是POJO
     *
     * @param jsonData json数据
     * @param clazz    ResultModel中的object类型
     * @return ResultModel
     */
    public static ResultModel formatToPOJO(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return format(jsonData);
            }
            JSONObject object = JSON.parseObject(jsonData);
            Object jsonPOJO = object.get("data");
            Object data = JSON.parseObject(jsonPOJO + "", clazz);
            return new ResultModel((Integer) object.get("code"), object.get("message") + "", data);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 没有object对象的转化
     *
     * @param jsonData 待转化的json字符串
     * @return ResultModel
     */
    public static ResultModel format(String jsonData) {
        try {
            return JSON.parseObject(jsonData, ResultModel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public ResultModel put(String key,Object object){
        data = data==null?new JSONObject():data;
        JSONObject data2 = (JSONObject) data;
        data2.put(key,object);
        return this;
    }

    public int getCode() {
        return code;
    }

    public ResultModel setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public ResultModel setMsg(String message) {
        this.msg = message;
        return this;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }


}
